package bingosoft.metro.datasync.uuap.service;

import bingosoft.components.base.helper.ObjectHelper;
import bingosoft.components.base.service.Result;
import bingosoft.components.base.service.ResultEntity;
import bingosoft.metro.datasync.base.service.IncSyncService;
import bingosoft.metro.datasync.uuap.UuapClientFactory;
import bingosoft.metro.datasync.uuap.client.form.RoleDataForm;
import bingosoft.metro.datasync.uuap.client.form.RolePermissionDataForm;
import bingosoft.metro.datasync.uuap.client.param.RequestParam;
import bingosoft.metro.datasync.uuap.client.service.DataClientService;
import bingosoft.metro.datasync.uuap.model.SyncRoleModel;
import bingosoft.metro.datasync.uuap.model.SyncRolePermissionRefModel;
import leap.core.AppContext;
import leap.orm.model.Model;

import java.util.*;

/**
 * @author Feily
 * @功能描述 角色增量同步数据服务类
 * @创建时间 2017-12-13
 */
public class RoleSyncService extends IncSyncService {
	
	/**
	 * 角色角色关系列表
	 */
	protected List<SyncRolePermissionRefModel> rolePermissionRefs = new ArrayList<SyncRolePermissionRefModel>();
	
	/**
	 * 删除角色权限中间表SQL
	 */
	protected String deleteRolePermissionSyncDataSql;
	/**
	 * 删除角色权限目标表SQL
	 */
	protected String deleteRolePermissionTargetDataSql;
	/**
	 * 保存角色权限目标表SQL
	 */
	protected String saveRolePermissionTargetDataSql;
	
	/**
	 * 构建函数
	 */
	public RoleSyncService() {
		this.setLogClass(this.getClass());
		
		this.tenantCode = AppContext.config().getProperty("uuap.tenantCode");

		this.getLastSyncTimeSql = this.getSqlContent("uuap-role-getLastSyncTime");
		this.deleteSyncDataSql = this.getSqlContent("uuap-role-deleteSyncData");
		this.saveTargetDataSql = this.getSqlContent("uuap-role-saveTargetData");
		this.updateTargetDataSql = this.getSqlContent("uuap-role-updateTargetData");
		this.deleteTargetDataSql = this.getSqlContent("uuap-role-deleteTargetData");
		
		this.deleteRolePermissionSyncDataSql = this.getSqlContent("uuap-rolePermission-deleteSyncData");
		this.deleteRolePermissionTargetDataSql = this.getSqlContent("uuap-rolePermission-deleteTargetData");
		this.saveRolePermissionTargetDataSql = this.getSqlContent("uuap-rolePermission-saveTargetData");
	}

	@Override
	public Result deleteSyncData() {
		this.logMethodCalled("deleteSyncData");
	
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tenantCode", this.tenantCode);
		
		// 删除角色权限关系
		getDao().executeUpdate(this.deleteRolePermissionSyncDataSql, params);	
		
		// 删除角色
		getDao().executeUpdate(this.deleteSyncDataSql, params);	
		
		return new Result();
	}
	
	@Override
	public Result saveSyncData(List<Model> syncData){
		this.logMethodCalled("saveSyncData");
		
		// 保存角色
		getDao().batchInsert(syncData);
		
		// 保存角色权限关系
		getDao().batchInsert(this.rolePermissionRefs);

		return new Result();
	}
	
	@Override
	public Result syncTargetData() {
		this.logMethodCalled("syncTargetData");

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tenantCode", this.tenantCode);
		
		// 修改目标角色数据
		getDao().executeUpdate(this.updateTargetDataSql, params);
		
		// 保存目标角色数据
		getDao().executeUpdate(this.saveTargetDataSql, params);
		
		// 删除目标角色权限数据
		getDao().executeUpdate(this.deleteRolePermissionTargetDataSql, params);
		
		// 删除目标角色数据
		getDao().executeUpdate(this.deleteTargetDataSql, params);
		
		// 保存目标角色权限关系数据
		getDao().executeUpdate(this.saveRolePermissionTargetDataSql, params);
				
		return new Result();
	}
	
	@Override
	public List<Model> getSyncDataList(Date lastSyncTime) {
		this.logMethodCalled("getSyncDataList");
		this.logParamValue("lastSyncTime", lastSyncTime);
		
		List<Model> list = new ArrayList<Model>();
		
		DataClientService service  = UuapClientFactory.getDataClientService();
		
		RequestParam requestParam = new RequestParam();
		requestParam.setUpdateBeginDate(lastSyncTime);
		requestParam.setUpdateEndDate(new Date());
		
		ResultEntity<List<RoleDataForm>> resultEntity = service.queryRoles(requestParam);
		if(!resultEntity.isSuccess()){
			throw new RuntimeException(resultEntity.getMessage());
		}
		
		// 构建数据
		List<RoleDataForm> roles = resultEntity.getBusinessObject();
		for (RoleDataForm roleDataForm : roles) {
			
			// 构建角色
			SyncRoleModel role = new SyncRoleModel();
			roleDataForm.copyTo(role);
			role.setTenantCode(this.tenantCode);
			
			role.setSyncId(ObjectHelper.getUUID());
			role.setSyncTime(new Date());
			role.setIsSync(true);
			list.add(role);
			
			// 构建角色权限列表
			for (RolePermissionDataForm rolePermissionDataForm : roleDataForm.getPermissions()) {
				SyncRolePermissionRefModel ref = new SyncRolePermissionRefModel();
				ref.setRolePermissionId(rolePermissionDataForm.getRolePermissionId());
				ref.setPermissionId(rolePermissionDataForm.getPermissionId());
				ref.setPermissionRuleId(rolePermissionDataForm.getPermissionRuleId());
				
				ref.setRoleId(roleDataForm.getRoleId());
				ref.setCreateTime(roleDataForm.getCreateTime());
				ref.setCreateUserId(roleDataForm.getCreateUserId());
				ref.setUpdateTime(roleDataForm.getUpdateTime());
				ref.setUpdateUserId(roleDataForm.getUpdateUserId());
				ref.setTenantCode(this.tenantCode);
				
				ref.setSyncId(ObjectHelper.getUUID());
				ref.setSyncTime(new Date());
				ref.setIsSync(role.getIsSync());
				this.rolePermissionRefs.add(ref);
			}
		}
		
		return list;
	}

	public List<SyncRolePermissionRefModel> getRolePermissionRefs() {
		return rolePermissionRefs;
	}

	public void setRolePermissionRefs(List<SyncRolePermissionRefModel> rolePermissionRefs) {
		this.rolePermissionRefs = rolePermissionRefs;
	}
}
